<?php

/**
 * This is the model class for table "xman_task".
 *
 * The followings are the available columns in table 'xman_task':
 * @property integer $task_id
 * @property string $task_name
 * @property string $task_description
 * @property integer $task_progress
 * @property string $task_status
 * @property integer $task_task_list_id
 * @property string $task_due_date
 * @property string $task_created_date
 * @property string $task_updated_date
 * @property string $task_completed_date
 * @property string $task_deleted_date
 * @property integer $task_priority
 * @property integer $task_created_by_user_id
 *
 * The followings are the available model relations:
 * @property TaskList $taskTaskList
 * @property User $taskCreatedByUser
 * @property TaskUser[] $taskUsers
 */
class Task extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Task the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_task';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('task_name, task_task_list_id, task_created_by_user_id', 'required'),
			array('task_progress, task_task_list_id, task_priority, task_created_by_user_id', 'numerical', 'integerOnly'=>true),
			array('task_name, task_status', 'length', 'max'=>50),
			array('task_description', 'length', 'max'=>100),
			array('task_due_date, task_created_date, task_updated_date, task_completed_date, task_deleted_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('task_id, task_name, task_description, task_progress, task_status, task_task_list_id, task_due_date, task_created_date, task_updated_date, task_completed_date, task_deleted_date, task_priority, task_created_by_user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'taskTaskList' => array(self::BELONGS_TO, 'TaskList', 'task_task_list_id'),
			'taskCreatedByUser' => array(self::BELONGS_TO, 'User', 'task_created_by_user_id'),
			'taskUsers' => array(self::HAS_MANY, 'TaskUser', 'task_user_task_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'task_id' => 'Task',
			'task_name' => 'Task Name',
			'task_description' => 'Task Description',
			'task_progress' => 'Task Progress',
			'task_status' => 'Task Status',
			'task_task_list_id' => 'Task Task List',
			'task_due_date' => 'Task Due Date',
			'task_created_date' => 'Task Created Date',
			'task_updated_date' => 'Task Updated Date',
			'task_completed_date' => 'Task Completed Date',
			'task_deleted_date' => 'Task Deleted Date',
			'task_priority' => 'Task Priority',
			'task_created_by_user_id' => 'Task Created By User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('task_id',$this->task_id);
		$criteria->compare('task_name',$this->task_name,true);
		$criteria->compare('task_description',$this->task_description,true);
		$criteria->compare('task_progress',$this->task_progress);
		$criteria->compare('task_status',$this->task_status,true);
		$criteria->compare('task_task_list_id',$this->task_task_list_id);
		$criteria->compare('task_due_date',$this->task_due_date,true);
		$criteria->compare('task_created_date',$this->task_created_date,true);
		$criteria->compare('task_updated_date',$this->task_updated_date,true);
		$criteria->compare('task_completed_date',$this->task_completed_date,true);
		$criteria->compare('task_deleted_date',$this->task_deleted_date,true);
		$criteria->compare('task_priority',$this->task_priority);
		$criteria->compare('task_created_by_user_id',$this->task_created_by_user_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        /**
         * List all assignment name
         */
        public function listAssignmentName(){
            // List user assigned with this task
            $listUsers = $this->taskUsers;
            $listUsersName = "";
            foreach ($listUsers as $user){
                $user = User::model()->findByPk($user->task_user_user_id);
                $listUsersName .= $user->user_name.". ";
            }
            return $listUsersName;
        }
        
        /**
         * Array priority
         */
        public function listPriority() {
        	return array(
        		0 => 'No Priority Selected',
        		1 => 'Low',
        		2 => 'Medium',
        		3 => 'High',
        	);
        }
}